package com.wu.printmatrix;

import java.util.ArrayList;

/**
 * 顺时针打印矩阵
 * @author lynn
 * @date 2020/7/31 - 17:00
 */
public class Solution {
    public ArrayList<Integer> printMatrix(int[][] matrix) {
        int up = 0;
        int right = matrix[0].length - 1;
        int down = matrix.length - 1;
        int left = 0;
        ArrayList<Integer> list = new ArrayList<Integer>();
        while (true) {
            for (int col = left; col <= right; col++) {
                list.add(matrix[up][col]);
            }
            up++;
            if (up > down) {
                break;
            }
            for (int row = up; row <= down; row++) {
                list.add(matrix[row][right]);
            }
            right--;
            if (right < left) {
                break;
            }
            for (int col = right; col >= left; col--) {
                list.add(matrix[down][col]);
            }
            down--;
            if (down < up) {
                break;
            }
            for (int row = down; row >= up; row--) {
                list.add(matrix[row][left]);
            }
            left++;
            if (left > right) {
                break;
            }
        }
        return list;
    }
}
